package com.llmm.eduService.config;


import com.llmm.exception.CommonErrorCode;
import com.llmm.exception.CustomException;
import com.llmm.exception.RestErrorResponse;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

//全局异常处理类
@ControllerAdvice//该异常对controller切面的增强
public class GlobalExceptionHandler {


    //捕获Exception异常
    @ExceptionHandler(value = Exception.class) //指定该全局异常处理类的这个方法来出处理异常。
    @ResponseBody
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public RestErrorResponse processException(Exception exception) {
        //判断异常是否为自定义异常。
        if (exception instanceof CustomException) {
            CustomException be = (CustomException) exception;
            RestErrorResponse response = new RestErrorResponse(be.getErrorCode().getCode(),
                    be.getErrorCode().getDesc());
            System.out.println(((CustomException) exception).getErrorCode());
            System.out.println(((CustomException) exception).getErrorCode());
            return response;
        }
        exception.printStackTrace();
        return new RestErrorResponse(CommonErrorCode.UNKNOWN.getCode(), CommonErrorCode.UNKNOWN.getDesc());
    }
}
